<?php
	// -----------   Page Vars    ----------
	$to_root = '';
	$authorized = array(0, 1);

	$menu_selected = 3;
	
	//  ---- init
	include($to_root.'_classes/init.php');
	
	$main = new ViewDescriptor('main');
	Template::addBreadcrumb('home.php', 'Home');
	
	/* ========   INSERT CODE HERE   ======== */
	//---- POST
	if(isset($_POST['action']) && isset($_POST['id']) && isset($_POST['title']) && isset($_POST['text'])){
		$id = $_POST['id'];
		$title = $_POST['title'];
		$text = $_POST['text'];
		if($_POST['action'] == 'todo'){						//----------------   New Message to Todo
			$ret = $GLOBALS['Db-I']->newMessage($title, $text, -1, $id);
			if(!is_array($ret)){
				header('Location: discussions.php?tID='.$_GET['tID'].'&msg=insert_success');
			} else {
				if(isset($ret['Error']) && $ret['Error'] == ERROR_NOT_ALLOWED){
					header('Location: discussions.php?tID='.$_GET['tID'].'&msg=not_authorized');
				} else {
					header('Location: discussions.php?tID='.$_GET['tID'].'&msg=update_error');
				}
			}
		} else if($_POST['action'] == 'list'){						//----------------   New Message to List
			$ret = $GLOBALS['Db-I']->newMessage($title, $text, -1, -1, -1, $id);
			if(!is_array($ret)){
				header('Location: discussions.php?lID='.$_GET['lID'].'&msg=insert_success');
			} else {
				if(isset($ret['Error']) && $ret['Error'] == ERROR_NOT_ALLOWED){
					header('Location: discussions.php?lID='.$_GET['lID'].'&msg=not_authorized');
				} else {
					header('Location: discussions.php?lID='.$_GET['lID'].'&msg=update_error');
				}
			}
		} else if($_POST['action'] == 'user'){						//----------------   New Message to User
			$ret = $GLOBALS['Db-I']->newMessage($title, $text, -1, -1, $id, -1);
			if(!is_array($ret)){
				header('Location: discussions.php?uID='.$_GET['uID'].'&msg=insert_success');
			} else {
				if(isset($ret['Error']) && $ret['Error'] == ERROR_NOT_ALLOWED){
					header('Location: discussions.php?uID='.$_GET['uID'].'&msg=not_authorized');
				} else {
					header('Location: discussions.php?uID='.$_GET['uID'].'&msg=update_error');
				}
			}
		}
		
	}
	//----- RENDER
	$page = isset($_GET['page']) ? $_GET['page'] : 1;
	$pp = DISCUSSIONS_PER_PAGE;
	
	if(isset($_GET['lID'])){											//----------------------- VIEW listMessages
		$content = new ViewDescriptor('content/discussions_view');
		$allmsgs = -1;
		$pro = $GLOBALS['Db-I']->getProject($_GET['lID']);
		if(!is_array($pro)){
			Template::addBreadcrumb('projects.php?id='.$pro->getId(), $pro->getTitle());
			Template::addBreadcrumb('discussions.php?lID='.$pro->getId(), 'Nachrichten');
			$content->addValue('td_id', $pro->getId());
			$content->addValue('td_title', $pro->getTitle());
			$content->addValue('td_text', nl2br($pro->getDescription()));
			$content->addValue('kind', 'list');
			$content->addValue('link', 'projects.php');
			$content->addValue('idText', 'lID');
			
			$m = new ViewDescriptor('content/parts/message');
			$messages = '';
			$messages_r = $GLOBALS['Db-I']->getMessagesForList($_GET['lID'], ($page-1)*$pp, $pp, '', false);
			foreach($messages_r as $message){
				if($allmsgs == -1) $allmsgs = $message->getAllMsgCount();
				$m_s = new SubViewDescriptor('message');
				$m_s->addValue('id', $message->getId());
				$m_s->addValue('title', $message->getTitle());
				$m_s->addValue('text', nl2br($message->getText()));
				$m_s->addValue('date', $message->getDate());
				$m_s->addValue('a_id', $message->getAuthor()->getId());
				$m_s->addValue('reply_count', $message->getReplyCount());
				$m_s->addValue('a_name', $message->getAuthor()->getName());
				
				$m->addSubView($m_s);
			}
			if(count($messages_r)>0){
				$content->removeSubView('no_msgs');
			} else {
				if($page > 1) header('Location: discussions.php?lID='.$_GET['lID']);
				$nm = new SubViewDescriptor('no_msgs');
				$content->addSubView($nm);
				$m->removeSubView('message');
			}
			$content->addValue('pagina', Template::getPagina($page, $pp, $allmsgs, 'discussions.php?lID='.$pro->getId().'&page={@pp:page}'));
			$content->addValue('messages', $m->render());
		} else {
			if($pro['Error'] == ERROR_LIST_DOES_NOT_EXIST) header('Location: projects.php?msg=not_found');
			else header('Location: projects.php?msg=not_found');
		}
	} else if(isset($_GET['tID'])){											//----------------------- EDIT ? todoMessage
		$content = new ViewDescriptor('content/discussions_view');
		
		$todo = $GLOBALS['Db-I']->getTodo($_GET['tID']);
		if(!is_array($todo)){
			Template::addBreadcrumb('todos.php?id='.$todo->getId(), $todo->getTitle());
			Template::addBreadcrumb('discussions.php?tID='.$todo->getId(), 'Nachrichten');
			$content->addValue('td_id', $todo->getId());
			$content->addValue('td_title', $todo->getTitle());
			$content->addValue('td_text', nl2br($todo->getDescription()));
			$content->addValue('kind', 'todo');
			$content->addValue('link', 'todos.php');
			$content->addValue('idText', 'tID');
			
			$m = new ViewDescriptor('content/parts/message');
			$messages = '';
			$allmsgs = 0;
			$messages_r = $GLOBALS['Db-I']->getMessagesForTodo($_GET['tID'], ($page-1)*$pp, $pp, '', false);
			foreach($messages_r as $message){
				if($allmsgs == 0) $allmsgs = $message->getAllMsgCount();
				$m_s = new SubViewDescriptor('message');
				$m_s->addValue('id', $message->getId());
				$m_s->addValue('title', $message->getTitle());
				$m_s->addValue('text', nl2br($message->getText()));
				$m_s->addValue('date', $message->getDate());
				$m_s->addValue('a_id', $message->getAuthor()->getId());
				$m_s->addValue('reply_count', $message->getReplyCount());
				$m_s->addValue('a_name', $message->getAuthor()->getName());
				
				$m->addSubView($m_s);
			}
			if(count($messages_r)>0){
				$content->removeSubView('no_msgs');
			} else {
				if($page > 1) header('Location: discussions.php?tID='.$_GET['tID']);
				$nm = new SubViewDescriptor('no_msgs');
				$content->addSubView($nm);
				$m->removeSubView('message');
			}
			
			$content->addValue('pagina', Template::getPagina($page, $pp, $allmsgs, 'discussions.php?tID='.$todo->getId().'&page={@pp:page}'));
			$content->addValue('messages', $m->render());
		} else {
			if($todo['Error'] == ERROR_LIST_DOES_NOT_EXIST) header('Location: projects.php?msg=not_found');
			else header('Location: discussions.php?msg=not_found');
		}
	} else if(isset($_GET['uID'])){											//----------------------- EDIT  userMessage
		$content = new ViewDescriptor('content/discussions_view');
		if($_GET['uID'] == $_SESSION['User']['id']){
			$pro = $GLOBALS['Db-I']->getUserById($_GET['uID']);
			if(!is_array($pro)){
				Template::addBreadcrumb('profile.php?id='.$pro->getId(), $pro->getName());
				Template::addBreadcrumb('discussions.php?uID='.$pro->getId(), ' Nachrichten');
				$content->addValue('td_id', $pro->getId());
				$content->addValue('td_title', $pro->getName());
				$content->addValue('td_text', '');
				$content->addValue('kind', 'user');
				$content->addValue('link', 'profile.php');
				$content->addValue('idText', 'uID');
				
				$m = new ViewDescriptor('content/parts/message');
				$messages = '';
				$allmsgs = 0;
				$messages_r = $GLOBALS['Db-I']->getMessagesForUser($_GET['uID'], ($page-1)*$pp, $pp, '', false);
				//print_r($messages_r);
				if(count($messages_r) > 0 && !isset($messages_r['Error'])){
					foreach($messages_r as $message){
						if($message != '16'){
							if($allmsgs == 0) $allmsgs = $message->getAllMsgCount();
							$m_s = new SubViewDescriptor('message');
							$m_s->addValue('id', $message->getId());
							$m_s->addValue('title', $message->getTitle());
							$m_s->addValue('text', nl2br($message->getText()));
							$m_s->addValue('date', $message->getDate());
							$m_s->addValue('a_id', $message->getAuthor()->getId());
							$m_s->addValue('reply_count', $message->getReplyCount());
							$m_s->addValue('a_name', $message->getAuthor()->getName());
							
							$m->addSubView($m_s);
						}
					}
				} else $m->removeSubView('message');
				if(count($messages_r)>0){
					$content->removeSubView('no_msgs');
				} else {
					$nm = new SubViewDescriptor('no_msgs');
					$content->addSubView($nm);
					$m->removeSubView('message');
				}
				
				$content->addValue('pagina', Template::getPagina($page, $pp, $allmsgs, 'discussions.php?uID='.$pro->getId().'&page={@pp:page}'));
				$content->addValue('messages', $m->render());
			} else {
				if($pro['Error'] == ERROR_LIST_DOES_NOT_EXIST) header('Location: profile.php?msg=not_found');
				else header('Location: profile.php?msg=not_found');
			}
		} else header('location: discussions.php?uID='.$_SESSION['User']['id']);
	}  else header('Location: discussions.php?uID='.$_SESSION['User']['id']);					//

	
		
	/* ========   NORMAL STUFF   ========= */
	// Messages
	if(isset($_GET['msg']) && isset($errors) && is_array($errors) && isset($error_class) && is_array($error_class)){
    	$get_msg = explode('|', $_GET['msg']);
    	foreach($get_msg as $error){
    		if(isset($errors[$error]) && isset($error[$GLOBALS['Localization']['language']])){
    			$GLOBALS['Messages'][] = '<span class="msg_'.$error_class[$error].'">'.$errors[$error].'</span>';
    		}
    	}
    }
    // render Main Template
	Template::generateMenu($main, $menu_selected);
	Template::renderBreadCrumb($main);
	$main->addValue('messages', implode("\n", $GLOBALS['Messages']));
	$main->addValue('content', $content->render());
	$main->addValue('script', '<script type="text/javascript" src="'.$GLOBALS['tpl']['root'].'_template/todolist/'.$GLOBALS['Localization']['language'].'/js/discussions.js"></script>');
	$main->addValue('style', '<link rel="stylesheet" href="'.$GLOBALS['tpl']['root'].'_template/todolist/'.$GLOBALS['Localization']['language'].'/css/discussions.css" type="text/css"  media="screen"/>');
	
	echo $main->render(); 
?>